﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace Global
{
    public static class ControlSerializer
    {
        public static List<ControlInfo> GetAllControlsInfo(Control container)
        {
            var controlInfos = new List<ControlInfo>();

            foreach (Control control in container.Controls)
            {
                ControlInfo controlInfo = new ControlInfo();
                controlInfo.ControlName = control.Name;
                controlInfo.ControlType = control.GetType().Name;
                controlInfo.Properties = GetControlProperties(control);
                // 添加当前控件信息
                controlInfos.Add(controlInfo);

                // 递归处理子控件（如Panel、GroupBox等容器控件）
                if (control.HasChildren)
                {
                    controlInfos.AddRange(GetAllControlsInfo(control));
                }
            }
          //  Application.FilterMessage
            return controlInfos;
        }
        public static Dictionary<string, object> GetControlProperties(Control control)
        { 
            var properties = new Dictionary<string, object>();
            var controlType = control.GetType();

            // 获取所有公共属性
            foreach (var prop in controlType.GetProperties())
            {
                // 过滤掉我们不关心的属性
                if (prop.Name == "Name" || prop.Name == "Text" ||
                    prop.Name == "Checked" || prop.Name == "SelectedIndex" ||
                    prop.Name == "Enabled" || prop.Name == "Visible")
                {
                    try
                    {
                        properties[prop.Name] = prop.GetValue(control);
                    }
                    catch
                    {
                        // 忽略无法读取的属性
                    }
                }
            }

            return properties;
        }
        public static void SaveControlsToJson(Control container, string filePath)
        {
            var controlsInfo = GetAllControlsInfo(container);
            string json = JsonConvert.SerializeObject(controlsInfo, Formatting.Indented);
            File.WriteAllText(filePath, json);
        }

        public static List<ControlInfo> LoadControlsFromJson(string filePath)
        {
            if (!File.Exists(filePath))
                return new List<ControlInfo>();

            string json = File.ReadAllText(filePath);
            return JsonConvert.DeserializeObject<List<ControlInfo>>(json);
        }

        public static void ApplyControlsFromJson(Control container, string filePath)
        {
            var savedControls = LoadControlsFromJson(filePath);

            foreach (Control control in container.Controls)
            {
                // 查找匹配的保存的控件信息
                var savedInfo = savedControls.Find(c => c.ControlName == control.Name);
                if (savedInfo != null)
                {
                    // control.Text = savedInfo.Text;
                    var propertys = savedInfo.Properties;
                    Type controlType=control.GetType();
                    foreach (var propKV in propertys) { 
                       PropertyInfo prop=controlType.GetProperty(propKV.Key);
                        if (prop != null&&prop.CanWrite) {
                            try
                            {
                                prop.SetValue(control,propKV.Value);
                            }
                            catch (Exception)
                            {

                                throw;
                            }
                        }
                    
                    }
                }

                // 递归处理子控件
                if (control.HasChildren)
                {
                    ApplyControlsFromJson(control, filePath);
                }
            }
        }
    }


    public class ControlInfo
    {
        public string ControlName { get; set; }
          public string ControlType { get; set; }
        //  public List<ControlProperty> Properties { get; set; } = new List<ControlProperty>();
        public Dictionary<string, object> Properties { get; set; } = new Dictionary<string, object>();
    }
}
